<?php
/**
 * 《请假信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/KqUtil.class.php');

class Vacation extends KqUtil {
	public $tbl = Constant::tbl_kq_vacation;

	public $vacation = array(
		'-' => '请选择',
		'1' => '事假',
		'2' => '年假',
		'3' => '病假',
		'4' => '调休',

		'5' => '婚假',
		'6' => '产假',
		'7' => '丧假',
		'8' => '工伤假',
		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在审批',
		'3' => '已经批准',
		'4' => '未批准',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'vacation' => '类型',
		't1' => '请假时间',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'emp' => '员工',
		'vacation' => '类型',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('vacation_option', $this->vacation);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '请假人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'kq/vacation/find');
		return true;
	}

	function _orderby_x($controller, $row, $px = '-') {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$vacation_s = $request['vacation_s'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$my = $this->_my();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($vacation_s) && $vacation_s <> '-') {
			$where .= " AND vacation = '" . $vacation_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
			// $where .= sprintf(" AND (t1 <= '%s' AND t2 > '%s')", $date1_s, $date1_s);
		}

		if (!empty($date2_s)) {
			$where .= " AND t1 < '" . $date2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'kq/vacation/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {

			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'kq/vacation/list');
		return true;
	}

	function emp($controller, $request) {
		$this->pretreat($controller);

		$emp_s = $request['id'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 <= '%s' ORDER BY t1";
		$sql = sprintf($fmt, $this->tbl, $emp_s, $date1_s, $date2_s);		debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$row['vacation_t'] = $this->vacation[$row['vacation']];

			$row['week'] = $this->week($row['t1']);
			$row['date'] = $this->_date($row['t1'], $row['t2']);

			$row['status_t'] = $this->status[$row['status']];

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'kq/vacation/emp');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/vacation/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$vacation = $request['vacation'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$hh = $request['hh'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, name, vacation, t1, t2, hh, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $vacation, $t1, $t2, $hh, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 审核
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/vacation/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/vacation/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$vacation = $request['vacation'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$hh = $request['hh'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', name = '%s', vacation = '%s', t1 = '%s', t2 = '%s', hh = '%s', content = '%s'";
		$fmt .= ", modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $vacation, $t1, $t2, $hh, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'kq/vacation/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'kq/vacation/upload');
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}

		if ($this->_audit('110')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		if (!empty($request['small'])) {
			$controller->display($request, 'kq/vacation/view_small');
			return true;
		}

		$controller->display($request, 'kq/vacation/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
			
		$row['vacation_t'] = $this->vacation[$row['vacation']];

		$row['week'] = $this->week($row['t1']);
		$row['date'] = $this->_date($row['t1'], $row['t2']);
		
		$row['files'] = $this->_files($row['id']);

		$row = $this->_view_t($row);

		return $row;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$emp = $request['emp'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC LIMIT 10";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
	
		$controller->display($request, 'kq/vacation/list_4');
		return true;
	}
	
	function approve($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'kq/vacation/approve');
		return true;
	}
	
	function approvePost($controller, $request) {
		return $this->verifyPost($controller, $request);
	}
	
	function batch_verify($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
	
		$controller->display($request, 'kq/vacation/batch_verify');
		return true;
	}
	
	function _batch_insql222($a) {
		// $a = json_decode($cc);
		if (!$a || $a == NULL) {
			debug("cc is NULL");
			return '';
		}
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($a)) {
			$insql .= "', '" . $v;
		}
		$insql .= "')";
	
		return $insql;
	}
	
	function batch_verifyPost($controller, $request) {
		$this->pretreat($controller);
	
		$content = addslashes($request['content']);
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
			debug($sql);
			$db->query($sql);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_approve($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
	
		$controller->display($request, 'kq/vacation/batch_approve');
		return true;
	}
	
	function batch_approvePost($controller, $request) {
		return $this->batch_verifyPost($controller, $request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));
		$controller->assign('t2', strtotime('+1 day'));
		
		$controller->assign('result_s', "300");

		$controller->display($request, 'kq/vacation/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'vacation');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'vacation') {
			$row['a'] = $this->vacation[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function ns($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status > 1 AND t1 < now() AND t2 > now() ORDER BY office, dept";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
	
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$row = $this->_orderby_x($controller, $row);
	
			$data[] = $row;
		}
	
		$from = $this->email;
		$subject = sprintf("%s请假情况", date("Y-m-d"));
	
		$controller->assign_by_ref('result', $data);
		$body = $this->_tj_html($controller, $request, 'kq/vacation/ns');
		echo $body;
	
		$bb = $this->_list_email('171');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
	
		$this->_tj_mail($from, $bb, $subject, $body);
	}
	
	function _has_vacation($emp) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE t1 < now() AND t2 > now() AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		// echo $sql . "\n";
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '3') {
				return $row['id'];
			}
		}
		
		return false;
	}
	
	function view_emp2($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'kq/vacation/_list_c');
		return true;
	}
	
}
?>